﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class EventDao
    {
        public bool Delete(SqlConnection conn, int eventId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE ");
            builder.Append("\t[dbo].[Attachment] ");
            builder.Append("SET ");
            builder.Append("\t[DeleteFlag] = 1 ");
            builder.Append("WHERE ");
            builder.Append("\t[AttachmentId] IN ");
            builder.Append("\t\t\t( ");
            builder.Append("\t\t\t\tSELECT ");
            builder.Append("\t\t\t\t\t[AttachmentId] ");
            builder.Append("\t\t\t\tFROM ");
            builder.Append("\t\t\t\t\t[dbo].[Event]  ");
            builder.Append("\t\t\t\tWHERE ");
            builder.Append("\t\t\t\t\t[EventId] = @EventId ");
            builder.Append("\t\t\t\t\tAND ISNULL([AttachmentId],'') <> ''\t\t\t\t\t ");
            builder.Append("\t\t\t) ");
            builder.Append("DELETE FROM [dbo].[Event] ");
            builder.Append("WHERE ");
            builder.Append("\t[EventId] = @EventId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@EventId", SqlDbType.Int) };
            commandParameters[0].Value = eventId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public int Insert(SqlConnection conn, EventData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("INSERT INTO [dbo].[Event] ");
            builder.Append("( ");
            builder.Append("\t[Subject], ");
            builder.Append("\t[Content], ");
            builder.Append("\t[AttachmentId], ");
            builder.Append("\t[UserId], ");
            builder.Append("\t[AddTime], ");
            builder.Append("\t[ViewCount], ");
            builder.Append("\t[ViewUser] ");
            builder.Append(") ");
            builder.Append("VALUES ");
            builder.Append("( ");
            builder.Append("\t@Subject, ");
            builder.Append("\t@Content, ");
            builder.Append("\t@AttachmentId, ");
            builder.Append("\t@UserId, ");
            builder.Append("\t@AddTime, ");
            builder.Append("\t@ViewCount, ");
            builder.Append("\t@ViewUser ");
            builder.Append(") ");
            builder.Append("SELECT SCOPE_IDENTITY() AS RetValue ");
            SqlParameter[] commandParameters = new SqlParameter[7];
            commandParameters[0] = new SqlParameter("@Subject", SqlDbType.NVarChar, 200);
            commandParameters[0].Value = data.Subject;
            commandParameters[1] = new SqlParameter("@Content", SqlDbType.NText);
            if (data.ContentIsNull)
            {
                commandParameters[1].Value = DBNull.Value;
            }
            else
            {
                commandParameters[1].Value = data.Content;
            }
            commandParameters[2] = new SqlParameter("@AttachmentId", SqlDbType.NVarChar, 50);
            if (data.AttachmentIdIsNull)
            {
                commandParameters[2].Value = DBNull.Value;
            }
            else
            {
                commandParameters[2].Value = data.AttachmentId;
            }
            commandParameters[3] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[3].Value = data.UserId;
            commandParameters[4] = new SqlParameter("@AddTime", SqlDbType.DateTime);
            commandParameters[4].Value = data.AddTime;
            commandParameters[5] = new SqlParameter("@ViewCount", SqlDbType.Int);
            commandParameters[5].Value = data.ViewCount;
            commandParameters[6] = new SqlParameter("@ViewUser", SqlDbType.NText);
            if (data.ViewUserIsNull)
            {
                commandParameters[6].Value = DBNull.Value;
            }
            else
            {
                commandParameters[6].Value = data.ViewUser;
            }
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, builder.ToString(), commandParameters);
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public DataTable SelectByEventId(SqlConnection conn, int eventId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE ");
            builder.Append("\t[dbo].[Event] ");
            builder.Append("SET ");
            builder.Append("\t[ViewCount] = [ViewCount] + 1 ");
            builder.Append("WHERE ");
            builder.Append("\t[EventId] = @EventId ");
            builder.Append("SELECT ");
            builder.Append("\t[Event].[EventId], ");
            builder.Append("\t[Event].[Subject], ");
            builder.Append("\t[Event].[Content], ");
            builder.Append("\t[Event].[AttachmentId], ");
            builder.Append("\t[Event].[UserId], ");
            builder.Append("\t[Event].[AddTime], ");
            builder.Append("\t[Event].[ViewCount], ");
            builder.Append("\t[Event].[ViewUser], ");
            builder.Append("\t[Users].[UserName], ");
            builder.Append("\t[Department].[DeptName] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Event], ");
            builder.Append("\t[dbo].[Users], ");
            builder.Append("\t[dbo].[Department] ");
            builder.Append("WHERE ");
            builder.Append("\t[Event].[EventId] = @EventId ");
            builder.Append("\tAND [Event].[UserId] = [Users].[UserId] ");
            builder.Append("\tAND [Users].[DeptId] = [Department].[DeptId] ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@EventId", SqlDbType.Int) };
            commandParameters[0].Value = eventId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Event";
            return set.Tables[0];
        }

        public DataTable SelectMyTable(SqlConnection conn)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT TOP 5 ");
            builder.Append("\t[EventId], ");
            builder.Append("\t[Subject], ");
            builder.Append("\t[Content], ");
            builder.Append("\t[AttachmentId], ");
            builder.Append("\t[UserId], ");
            builder.Append("\t[AddTime], ");
            builder.Append("\t[ViewCount], ");
            builder.Append("\t[ViewUser] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Event] ");
            builder.Append("ORDER BY ");
            builder.Append("\t[EventId] DESC ");
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString());
            set.Tables[0].TableName = "Event";
            return set.Tables[0];
        }

        public DataTable SelectPaged(SqlConnection conn, int pageSize, int pageindex, bool docount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("IF(@docount=1) ");
            builder.Append("\tSELECT ");
            builder.Append("\t\tCOUNT([Event].[EventId]) ");
            builder.Append("\tFROM ");
            builder.Append("\t\t[dbo].[Event],[dbo].[Users],[dbo].[Department] ");
            builder.Append("\tWHERE ");
            builder.Append("\t\t[Event].[UserId] = [Users].[UserId] ");
            builder.Append("\t\tAND [Users].[DeptId] = [Department].[DeptId] ");
            builder.Append("ELSE ");
            builder.Append("\tBEGIN ");
            builder.Append("\t\tDECLARE @IndexTable TABLE(ID int identity(1,1),PK int) ");
            builder.Append("\t\tDECLARE @PageLowerBound int ");
            builder.Append("\t\tDECLARE @PageUpperBound int ");
            builder.Append("\t\tSET @PageLowerBound=(@pageindex-1)*@pagesize ");
            builder.Append("\t\tSET @PageUpperBound=@PageLowerBound+@pagesize ");
            builder.Append("\t\tSET rowcount @PageUpperBound ");
            builder.Append("\t\t ");
            builder.Append("\t\tINSERT INTO @IndexTable(PK) ");
            builder.Append("\t\tSELECT ");
            builder.Append("\t\t\t[Event].[EventId] ");
            builder.Append("\t\tFROM ");
            builder.Append("\t\t\t[dbo].[Event],[dbo].[Users],[dbo].[Department] ");
            builder.Append("\t\tWHERE ");
            builder.Append("\t\t\t[Event].[UserId] = [Users].[UserId] ");
            builder.Append("\t\t\tAND [Users].[DeptId] = [Department].[DeptId] ");
            builder.Append("\t\tORDER BY ");
            builder.Append("\t\t\t[Event].[EventId] DESC ");
            builder.Append("\t\t ");
            builder.Append("\t\tSELECT ");
            builder.Append("\t\t\t[Event].*, ");
            builder.Append("\t\t\t[Users].[UserName], ");
            builder.Append("\t\t\t[Department].[DeptName], ");
            builder.Append("\t\t\t@pageindex AS pageindex ");
            builder.Append("\t\tFROM ");
            builder.Append("\t\t\t[dbo].[Event], ");
            builder.Append("\t\t\t[dbo].[Users], ");
            builder.Append("\t\t\t[dbo].[Department], ");
            builder.Append("\t\t\t@IndexTable t\t\t\t ");
            builder.Append("\t\tWHERE ");
            builder.Append("\t\t\t[Event].[EventId] = t.PK ");
            builder.Append("\t\t\tAND [Event].[UserId] = [Users].[UserId] ");
            builder.Append("\t\t\tAND [Users].[DeptId] = [Department].[DeptId]\t\t ");
            builder.Append("\t\t\tAND t.ID>@PageLowerBound  ");
            builder.Append("\t\t\tAND t.ID<=@PageUpperBound ");
            builder.Append("\t\tORDER BY ");
            builder.Append("\t\t\t[Event].[EventId] DESC ");
            builder.Append("\tEND ");
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@pagesize", SqlDbType.Int);
            commandParameters[0].Value = pageSize;
            commandParameters[1] = new SqlParameter("@pageindex", SqlDbType.Int);
            commandParameters[1].Value = pageindex;
            commandParameters[2] = new SqlParameter("@docount", SqlDbType.Bit);
            commandParameters[2].Value = docount;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Event";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, EventData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE [dbo].[Event] ");
            builder.Append("SET ");
            builder.Append("\t[Subject] = @Subject, ");
            builder.Append("\t[Content] = @Content, ");
            builder.Append("\t[AttachmentId] = @AttachmentId, ");
            builder.Append("\t[UserId] = @UserId, ");
            builder.Append("\t[AddTime] = @AddTime, ");
            builder.Append("\t[ViewCount] = @ViewCount, ");
            builder.Append("\t[ViewUser] = @ViewUser ");
            builder.Append("WHERE ");
            builder.Append("\t[EventId] = @EventId ");
            SqlParameter[] commandParameters = new SqlParameter[8];
            commandParameters[0] = new SqlParameter("@EventId", SqlDbType.Int);
            commandParameters[0].Value = data.EventId;
            commandParameters[1] = new SqlParameter("@Subject", SqlDbType.NVarChar, 200);
            commandParameters[1].Value = data.Subject;
            commandParameters[2] = new SqlParameter("@Content", SqlDbType.NText);
            if (data.ContentIsNull)
            {
                commandParameters[2].Value = DBNull.Value;
            }
            else
            {
                commandParameters[2].Value = data.Content;
            }
            commandParameters[3] = new SqlParameter("@AttachmentId", SqlDbType.NVarChar, 50);
            if (data.AttachmentIdIsNull)
            {
                commandParameters[3].Value = DBNull.Value;
            }
            else
            {
                commandParameters[3].Value = data.AttachmentId;
            }
            commandParameters[4] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[4].Value = data.UserId;
            commandParameters[5] = new SqlParameter("@AddTime", SqlDbType.DateTime);
            commandParameters[5].Value = data.AddTime;
            commandParameters[6] = new SqlParameter("@ViewCount", SqlDbType.Int);
            commandParameters[6].Value = data.ViewCount;
            commandParameters[7] = new SqlParameter("@ViewUser", SqlDbType.NText);
            if (data.ViewUserIsNull)
            {
                commandParameters[7].Value = DBNull.Value;
            }
            else
            {
                commandParameters[7].Value = data.ViewUser;
            }
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }
    }
}

